home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / nextrad.lha / NeXtRad / VIEWPPM / viewppm.m < prev   
Encoding:
Text File  |  1993-02-22  |  2.6 KB  |  100 lines

  1. /* viewppm.m */
  2. /* written by : Jason R. Wilson */
  3. /* last modified : 2/20/93 */
  4. /* this package provides a simple .ppm file viewer. */
  5. /* It is provided to give users a way of viewing output */
  6. /* from the NeXtrad package */
  7.  
  8. #import <appkit/Application.h>
  9. #import <appkit/Window.h>
  10. #import <appkit/View.h>
  11. #import <appkit/Menu.h>
  12. #import <appkit/NXBitmapImageRep.h>
  13. #import <appkit/graphics.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17.  
  18. void DoWork(void)
  19. {
  20.     id myWindow, myView, myMenu, myImage; /* instance variables */
  21.     unsigned char *ScreenBuf; /* used to hold the data for the NXBitmapImageRep */
  22.     int loop; /* simple loop counter */
  23.     FILE *fp; /* file pointer to the ppm file */
  24.     char filename[256]; /* the name of the file to be viewed */
  25.     char foo[256]; /* a trash string used to read in P6 (assumed) from top of .ppm file */
  26.     NXRect graphicsRect; /* holds the view window coordinates of the image */
  27.     int width; /* the width in pixels of the image */
  28.     int height;/* the height in pixels of the image */
  29.     int maxValue; /* the maximum color value of the .ppm file (assumed to be 255) */
  30.     unsigned char r,g,b; /* the red, green, and blue values corresponding to a pixel in the image */
  31.  
  32.     printf ("Please input name of .ppm file to be viewed:\n");
  33.     scanf  ("%s",filename);
  34.  
  35.     fp = fopen (filename,"r");
  36.  
  37.     /* read in colors */
  38.     
  39.     fscanf (fp,"%s",foo);
  40.     fscanf (fp,"%d %d",&width,&height);
  41.     fscanf (fp,"%d",&maxValue);
  42.         fscanf (fp,"%c",&r);
  43.     ScreenBuf = (unsigned char *)(malloc(width*height*3));
  44.     for (loop = 0;loop < width*height*3;loop += 3)
  45.       {
  46.         fscanf (fp,"%c%c%c",&r,&g,&b);
  47.         ScreenBuf[loop] = r;
  48.         ScreenBuf[loop+1] = g;
  49.         ScreenBuf[loop+2] = b;
  50.       }
  51.       
  52.     /* set up window */
  53.     
  54.     NXSetRect(&graphicsRect,100.0,350.0,(NXCoord)width,(NXCoord)height);
  55.     myWindow = [[Window alloc] initContent:&graphicsRect
  56.             style:NX_TITLEDSTYLE
  57.             backing:NX_BUFFERED
  58.             buttonMask:NX_MINIATURIZEBUTTONMASK
  59.             defer:NO];
  60.     [myWindow setTitle:filename];
  61.     [myWindow display];
  62.  
  63.     /* set up menus */
  64.     myMenu = [[Menu alloc] initTitle:"viewppm"];
  65.     [myMenu addItem:"Quit" action:@selector(terminate:)
  66.         keyEquivalent:'q'];
  67.     [myMenu sizeToFit];
  68.     [NXApp setMainMenu:myMenu];
  69.  
  70.     /* set up object to hold and draw bitmap */
  71.  
  72.     myImage =  [[NXBitmapImageRep alloc]    initData:ScreenBuf
  73.                 pixelsWide:width
  74.                 pixelsHigh:height
  75.                 bitsPerSample:8
  76.                 samplesPerPixel:3
  77.                 hasAlpha:NO
  78.                 isPlanar:NO
  79.                 colorSpace:2
  80.                 bytesPerRow:width*3
  81.                 bitsPerPixel:24];
  82.  
  83.     [myImage draw];
  84.  
  85.  
  86.     myView = [[View alloc] initFrame:&graphicsRect];
  87.     [myView setOpaque:YES];
  88.     [myWindow setContentView:myView];
  89.     [myWindow makeKeyAndOrderFront:nil];              
  90.  
  91. }
  92.  
  93. void main()
  94. {
  95.     [Application new];
  96.     DoWork();
  97.     [NXApp run];
  98.     [NXApp free];
  99. }
  100.